// Copy Image Data
function copyImageData(ctx, src) {
    var dst = ctx.createImageData(src.width, src.height);
	try
	{
		dst.data.set(src.data);
	}
	catch(err)
	{
		for (var i = 0; i < src.data.length; ++i)
			dst.data[i] = src.data[i];
	}
	
    return dst;
}

// het scaled image size (to fit in div)
function getScaledImageSize(imageWidth, imageHeight, divWidth, divHeight) {
    // calculate difference between Div and Image size
    var widthDiff = imageWidth - divWidth;
    var heightDiff = imageHeight - divHeight;

    var scale = 1.0;
    if (widthDiff > 0 || heightDiff > 0) // image is bigger than div
    {
        // scale to fit width
        if (widthDiff > heightDiff) {
            scale = divWidth / imageWidth;
        }
        // scale to fit height
        else {
            scale = divHeight / imageHeight;
        }
    }
    var size = {};
    size.width = imageWidth * scale;
    size.height = imageHeight * scale;

    return size;
}